package com.common.zrd.mapper.base;

import cn.hutool.core.util.ArrayUtil;
import com.common.zrd.contract.MapperContract;
import com.common.zrd.validation.CommonValidation;
import com.common.zrd.validation.ValidationUtil;
import com.common.zrd.validation.errorcode.BaseErrorCodes;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zrd
 * @Date: 2020/4/15 09:33
 * @Description:
 */
public abstract class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapperZrd<T> mapper;

    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public T selectByPrimaryKey(Object o) {
        CommonValidation.isNull(o, BaseErrorCodes.CHECK_ARG_NULL, "查询单条数据", "主键");
        T rs = mapper.selectByPrimaryKey(o);
        CommonValidation.isNull(rs, BaseErrorCodes.DATABASE_QUERY_ONE_ERROR, "主键异常");
        return rs;
    }

    @Override
    public int deleteByPrimaryKey(T o) {
        CommonValidation.isNull(o, BaseErrorCodes.CHECK_ARG_NULL, "查询单条数据", "主键");
        int changeCounts = mapper.deleteByPrimaryKey(o);
        CommonValidation.isFalse(changeCounts > 0, BaseErrorCodes.DATABASE_DELETE_ERROR, "删除记录请求错误！");
        return changeCounts;
    }

    /**
     * 将一组数据平均分成n组
     *
     * @param source 要分组的数据源
     * @param n      平均分成n组
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int remainder = source.size() % n;  //(先计算出余数)
        int number = source.size() / n;  //然后是商
        int offset = 0;//偏移量
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

    @Override
    public int insertList(List<T> t) {
        CommonValidation.isEmpty(t, BaseErrorCodes.CHECK_ARG_NULL, "批量插入", "集合");

        List<List<T>> list = averageAssign(t, MapperContract.BATH_OPT_DATA);
        int changeCounts = 0;
        for (List<T> listT : list
        ) {
            changeCounts += mapper.insertList(listT);
        }
        CommonValidation.isFalse(changeCounts > 0, BaseErrorCodes.DATABASE_DELETE_ERROR, "批量插入请求异常！");
        return changeCounts;
    }

    @Override
    public int updateByPrimaryKey(T t) {
        return mapper.updateByPrimaryKey(t);

    }

    @Override
    public T selectOne(T t) {
        return mapper.selectOne(t);
    }

    @Override
    public List<T> select(T t) {
        return mapper.select(t);
    }

    @Override
    public List<T> seletCommonList(T t) {
        return mapper.seletCommonList(t);
    }

    @Override
    public int insert(T t) {
        ValidationUtil.ValidResult validResult = ValidationUtil.validateBean(t);
        CommonValidation.isTrue(validResult.hasErrors(), BaseErrorCodes.CHECK_ARG_NULL, "创建新的记录请求错误", validResult.getErrors());
        int changeCounts = mapper.insert(t);
        CommonValidation.isFalse(changeCounts > 0, BaseErrorCodes.DATABASE_DELETE_ERROR, "创建新的记录请求错误！");
        return changeCounts;
    }


    @Override
    public int delete(T t) {
        return mapper.delete(t);
    }

    @Override
    public int selectCount(T t) {
        return mapper.selectCount(t);
    }

    @Override
    public int insertSelective(T t) {
        ValidationUtil.ValidResult validResult = ValidationUtil.validateBean(t);
        CommonValidation.isTrue(validResult.hasErrors(), BaseErrorCodes.CHECK_ARG_NULL, "更新数据校验异常", validResult.getErrors());
        int changeCounts = mapper.insertSelective(t);
        CommonValidation.isFalse(changeCounts > 0, BaseErrorCodes.DATABASE_DELETE_ERROR, "创建新的记录请求错误！");
        return changeCounts;
    }

    @Override
    public int selectCountByExample(Example example) {
        return mapper.selectCountByExample(example);
    }

    @Override
    public List<T> selectByExample(Example example) {
        return mapper.selectByExample(example);
    }

    @Override
    public int deleteByExample(Example example) {
        return mapper.deleteByExample(example);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        CommonValidation.isTrue(ArrayUtil.isEmpty(ids), BaseErrorCodes.CHECK_ARG_NULL, "批量删除主键异常校验异常", "ids");
        return mapper.deleteByIds(ArrayUtil.join(ids, ","));
    }

    @Override
    public int updateByExample(T t, Example example) {

        return mapper.updateByExample(t, example);
    }

    @Override
    public int updateByPrimaryKeySelective(T t) {

        int changeCounts = mapper.updateByPrimaryKeySelective(t);
        CommonValidation.isFalse(changeCounts > 0, BaseErrorCodes.DATABASE_UPDATE_ERROR, "更新记录请求错误！");
        return changeCounts;
    }

    @Override
    public int updateByExampleSelective(T t, Example example) {


        return mapper.updateByExampleSelective(t, example);
    }

    @Override
    public T selectOneByExample(Example example) {
        return mapper.selectOneByExample(example);
    }

    @Override
    public List<T> selectByRowBounds(T t, RowBounds rowBounds) {
        return mapper.selectByRowBounds(t, rowBounds);
    }

    @Override
    public List<T> selectByExampleAndRowBounds(Example example, RowBounds rowBounds) {
        return mapper.selectByExampleAndRowBounds(example, rowBounds);
    }

    @Override
    public int updateList(List<T> t) {
        CommonValidation.isEmpty(t, BaseErrorCodes.CHECK_ARG_NULL, "批量更新", "集合");
        int changeCounts = mapper.updateList(t);


//        CommonValidation.isFalse(changeCounts > 0, BaseErrorCodes.DATABASE_DELETE_ERROR, "批量更新请求异常！");
        return changeCounts;
    }

}
